package com.my.factory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WangLuoXin
 * &#064;Date 2024/9/12  13:40
 * Handler注解 工厂处理器
 **/
public class HandlerBeanFactory {

    /**
     * Handler Beans
     */
    private static final List<Object> BEANS = new ArrayList<>(1024);

    public static Object getBean(Class<?> clazz) {
        for (Object bean : BEANS) {
            Class<?> aClass = bean.getClass();
            if (aClass.equals(clazz)) {
                return bean;
            }
        }

        try {
            Object o = clazz.newInstance();
            BEANS.add(o);
            return o;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<Object> getBeans(){
        return BEANS;
    }
}
